package com.tienon.message.mq;

import java.io.IOException;
import java.io.InvalidClassException;
import java.io.OptionalDataException;
import java.io.StreamCorruptedException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.ibm.mq.MQC;
import com.ibm.mq.MQEnvironment;
import com.ibm.mq.MQException;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQQueue;
import com.ibm.mq.MQQueueManager;
import com.tienon.message.entity.Push_App;

@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class ReceiveDataFromMQ {

	static {
		MQEnvironment.hostname = "localhost";
		MQEnvironment.port = 1417;
		MQEnvironment.CCSID = 1381;
		MQEnvironment.channel = "Server";// 服务器通道

	}
	private MQQueueManager manager = null;
	private MQQueue queue = null;

	/**
	 * 每次首次使用必须调用
	 * 
	 * @param qMgr
	 * @param q
	 * @throws MQException
	 */
	public void openMQ(String qMgr, String q) throws MQException {
		manager = new MQQueueManager(qMgr);
		// 必须要写MQC.MQOO_INQUIRE，否者无法查询队列深度（即消息条数）
		queue = manager.accessQueue(q, MQC.MQOO_INPUT_AS_Q_DEF
				| MQC.MQOO_INQUIRE);
	}

	public void commit() throws MQException {
		/*
		 * 把commit交代在最后是为了， 好与数据库操作的事务 起到一致的作用
		 */
		manager.commit();

	}

	public boolean isMQNull() {

		if (manager == null || queue == null) {
			return true;
		} else {
			return false;
		}

	}

	public boolean isManagerConnectedAndOpen() {

		return manager.isConnected && manager.isOpen;

	}

	public boolean isQueueOpen() {

		return queue.isOpen;

	}

	/**
	 * 每次结束必须调用
	 * 
	 * @throws MQException
	 */
	public void closeMQ() throws MQException {

		queue.close();
		manager.disconnect();

	}

	public Object receivedMsg() throws MQException, StreamCorruptedException,
			OptionalDataException, ClassNotFoundException, IOException {

		MQMessage msg = new MQMessage();
		queue.get(msg);

		Object obj = msg.readObject();

		return obj;

	}

	public List<Object> receiveAllMsgs() throws MQException,
			StreamCorruptedException, OptionalDataException,
			ClassNotFoundException, IOException {

		List<Object> datas = new ArrayList<Object>();

		while (queue.getCurrentDepth() > 0) {

			MQMessage msg = new MQMessage();
			queue.get(msg);
			datas.add(msg.readObject());

		}
		for(Object data : datas){			
			if(data instanceof Push_App){
				Push_App app = (Push_App) data;
				System.out.println("接收app消息: 批次号:" + app.getBatId() + " 流水号:"+app.getSerial()+" 序列号:" + app.getSplitId() + " 发送时间:"+ app.getSendTime() +" 发送的次数:"+app.getSendTimes()+" 发送的消息是:" + app.getMsg()+" 发送消息的优先级:"+app.getPrior());
				
			}			
		}
		return datas;
	}

	public List<Object> receivePartMsgs(int sum) throws MQException,
			InvalidClassException, StreamCorruptedException,
			OptionalDataException, ClassNotFoundException, IOException {

		List<Object> datas = new ArrayList<Object>();

		while (sum > 0) {

			MQMessage msg = new MQMessage();
			queue.get(msg);
			datas.add(msg.readObject());
			sum--;

		}

		return datas;

	}

}
